package com.example.idachuappone.order.item;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.order.action.OrderAction;
import com.example.idachuappone.order.imageutil.Bimp;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class MyOrderImageDeleteActivity extends BaseActivity implements OnClickListener {
	private MyPagerAdapter adapter;
	private Button btn_back;
	private Button btn_index;
	private BitmapUtils bu;
	private int index;
	private List<View> listViews;
	private OrderAction orderAction;
	private int request;
	private TextView tv_title;
	private ViewPager viewPager;

	private class MyPagerAdapter extends PagerAdapter {
		private List<View> list;

		private MyPagerAdapter() {
		}

		public void setList(List<View> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			return this.list == null ? 0 : this.list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView(this.list.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			((ViewPager) container).addView(this.list.get(position));
			return this.list.get(position);
		}
	}

	public MyOrderImageDeleteActivity() {
		this.request = 255;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_order_image_delete);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.orderAction = new OrderAction();
		this.index = getIntent().getIntExtra("index", 0);
		initView();
		initData();
		initAdapter();
		Log.e("aaa", new StringBuilder(String.valueOf(this.index)).toString());
		this.viewPager.setCurrentItem(this.index);
		Log.e("aaa", new StringBuilder(String.valueOf(this.index)).toString());
		this.tv_title.setText(new StringBuilder(String.valueOf(this.index + 1)).append("/").append(Bimp.drr.size())
				.toString());
	}

	private void initView() {
		this.tv_title = (TextView) findViewById(R.id.tv_title);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setOnClickListener(this);
		this.viewPager = (ViewPager) findViewById(R.id.viewpager);
		this.adapter = new MyPagerAdapter();
		this.viewPager.setAdapter(this.adapter);
		this.viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				MyOrderImageDeleteActivity.this.tv_title.setText((arg0 + 1) + "/" + Bimp.drr.size());
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	private void initData() {
		this.listViews = new ArrayList();
		for (int i = 0; i < Bimp.drr.size(); i++) {
			View view = LinearLayout.inflate(this, R.layout.simple_cook_greens_item, null);
			this.bu.display((ImageView) view.findViewById(R.id.img), Bimp.drr.get(i));
			LinearLayout ll_finish2 = (LinearLayout) view.findViewById(R.id.ll_finish2);
			((LinearLayout) view.findViewById(R.id.ll_finish1))
					.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			ll_finish2.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.listViews.add(view);
		}
	}

	private void initAdapter() {
		this.adapter.setList(this.listViews);
		this.adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_index /* 2131165213 */:
			AppShareData.isRefreshImageUpload = true;
			int page = this.orderAction.getPage(this.viewPager.getCurrentItem());
			if (page == -1) {
				setResult(0);
				finish();
				return;
			}
			Intent intent = new Intent(this, MyOrderImageDeleteActivity.class);
			intent.putExtra("index", page);
			startActivityForResult(intent, this.request);
		default:
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (this.request == arg0 && arg1 == 0) {
			setResult(0);
			finish();
		}
	}
}
